package com.rui.controller;

import com.rui.entity.Manager;
import com.rui.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * Created by Administrator on 2017/4/21.
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @RequestMapping("/add")
    public String add(Manager manager){
        managerService.add(manager);
        return "manager/list";
    }

    @RequestMapping("/check")
    @ResponseBody
    public Boolean check(String userName, Integer id){
        List<Manager> managers = managerService.findManagers(userName, id);
        boolean pass;
        if (managers.size()>0){
            pass = false;
        }else{
            pass = true;
        }
        return pass;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        Manager manager = managerService.findOne(id);
        managerService.delete(manager.getId());
        return "manager/list";
    }

    @RequestMapping("/detail")
    public String detail(Integer id){
        Manager manager = managerService.findOne(id);
        return "manager/edit";
    }

    @RequestMapping("/list")
    public String index(String realName, Integer pageSize, Integer pageIndex, Model model){
        List<Manager> managers = managerService.findManagers(realName,pageSize,pageIndex);
        Integer totalPage = managerService.findTotalPage(realName, pageSize);
        model.addAttribute("managers", managers);
        model.addAttribute("totalPage", totalPage);
        return "manager/list";
    }

    @RequestMapping("/update")
    public String update(Manager manager){
        managerService.update(manager);
        return "manager/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "manager/add";
    }
}
